package com.dzl.supplier.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.brand.pojo.WelfareBrand;
import com.dzl.brand.service.WelfareBrandService;
import com.dzl.constant.CommonConstants;
import com.dzl.supplier.dao.WelfareSupplierBrandMapper;
import com.dzl.supplier.pojo.WelfareSupplier;
import com.dzl.supplier.pojo.WelfareSupplierBrand;
import com.dzl.supplier.pojo.query.WelfareSupplierBrandQuery;
import com.dzl.supplier.service.WelfareSupplierBrandService;
import com.dzl.supplier.service.WelfareSupplierService;
import com.dzl.util.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商品牌关联表(WelfareSupplierBrand)表服务实现类
 * @author wangjian
 * @date 2021-07-12 10:45:53
 */
@Service
public class WelfareSupplierBrandServiceImpl extends BaseServiceImpl<WelfareSupplierBrand> implements WelfareSupplierBrandService {

    @Autowired
    private WelfareSupplierBrandMapper mapper;
    @Autowired
    private WelfareSupplierService welfareSupplierService;
    @Autowired
    private WelfareBrandService welfareBrandService;

    @Override
    public Map<String, Object> selectSupplierBrandVo(WelfareSupplierBrandQuery welfareSupplierBrand) {
        Map<String, Object> map = new HashMap<>();
        if(EmptyUtils.isNotEmpty(welfareSupplierBrand.getSupplierId())){
            //查所有供应商，并根据supplierId查关联品牌
            WelfareSupplier welfareSupplier = new WelfareSupplier();
            welfareSupplier.setStatus(CommonConstants.YES);
            if (welfareSupplierBrand.getIsApi() != null){
                welfareSupplier.setIsApi(welfareSupplierBrand.getIsApi());
            }
            welfareSupplier.setEnterpriseId(welfareSupplierBrand.getEnterpriseId());
            List<WelfareSupplier> supplierList = welfareSupplierService.select(welfareSupplier);
            //查品牌
            List<WelfareBrand> brandList = mapper.selectBrandListBySupplierId(welfareSupplierBrand.getSupplierId());
            map.put("brandList",brandList);
            //如果是企业端，则默认添加第一个供应商为福力兔
            if(welfareSupplierBrand.getContainFulitu() == CommonConstants.YES && !welfareSupplierBrand.getEnterpriseId().equals(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)){
                List<WelfareSupplier> supplierList_ = new ArrayList<>();
                WelfareSupplier welfareSupplier1 = new WelfareSupplier();
                welfareSupplier1.setId(null);
                welfareSupplier1.setName(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_NAME);
                supplierList_.add(welfareSupplier1);
                supplierList_.addAll(supplierList);
                map.put("supplierList",supplierList_);
            }else{
                map.put("supplierList",supplierList);
            }
        }else if(EmptyUtils.isNotEmpty(welfareSupplierBrand.getBrandId())){
            //查所有品牌，并根据brandId查关联供应商
            WelfareBrand welfareBrand = new WelfareBrand();
            welfareBrand.setStatus(CommonConstants.YES);
            //welfareBrand.setEnterpriseId(welfareSupplierBrand.getEnterpriseId());
            List<WelfareBrand> brandList = welfareBrandService.select(welfareBrand);
            //查供应商
            List<WelfareSupplier> supplierList = mapper.selectSupplierListByBrandIdAndEnterpriseId(welfareSupplierBrand.getBrandId(),welfareSupplierBrand.getEnterpriseId());
            map.put("brandList",brandList);
            //如果是企业端，则默认添加第一个供应商为福力兔
            if(welfareSupplierBrand.getContainFulitu() == CommonConstants.YES && !welfareSupplierBrand.getEnterpriseId().equals(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)){
                List<WelfareSupplier> supplierList_ = new ArrayList<>();
                WelfareSupplier welfareSupplier1 = new WelfareSupplier();
                welfareSupplier1.setId(null);
                welfareSupplier1.setName(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_NAME);
                supplierList_.add(welfareSupplier1);
                supplierList_.addAll(supplierList);
                map.put("supplierList",supplierList_);
            }else{
                map.put("supplierList",supplierList);
            }
        }else{
            //查所有品牌和所有供应商
            //查所有供应商
            WelfareSupplier welfareSupplier = new WelfareSupplier();
            welfareSupplier.setStatus(CommonConstants.YES);
            if (welfareSupplierBrand.getIsApi() != null){
                welfareSupplier.setIsApi(welfareSupplierBrand.getIsApi());
            }
            welfareSupplier.setEnterpriseId(welfareSupplierBrand.getEnterpriseId());
            List<WelfareSupplier> supplierList = welfareSupplierService.select(welfareSupplier);
            //查所有品牌
            WelfareBrand welfareBrand = new WelfareBrand();
            welfareBrand.setStatus(1);
            //welfareBrand.setEnterpriseId(welfareSupplierBrand.getEnterpriseId());
            List<WelfareBrand> brandList = welfareBrandService.select(welfareBrand);
            map.put("brandList",brandList);
            //如果是企业端，则默认添加第一个供应商为福力兔
            if(welfareSupplierBrand.getContainFulitu() == CommonConstants.YES && !welfareSupplierBrand.getEnterpriseId().equals(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)){
                List<WelfareSupplier> supplierList_ = new ArrayList<>();
                WelfareSupplier welfareSupplier1 = new WelfareSupplier();
                welfareSupplier1.setId(null);
                welfareSupplier1.setName(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_NAME);
                supplierList_.add(welfareSupplier1);
                supplierList_.addAll(supplierList);
                map.put("supplierList",supplierList_);
            }else{
                map.put("supplierList",supplierList);
            }
        }
        return map;
    }

    @Override
    public List<WelfareBrand> selectBrandListBySupplierId(Integer supplierId) {
        return mapper.selectBrandListBySupplierId(supplierId);
    }


}